package com.ruoyi.flowable.controller;

import java.util.List;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;

import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.vo.FormDataVo;
import com.ruoyi.flowable.domain.bo.FormDataBo;
import com.ruoyi.flowable.service.IFormDataService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单提交数据
 *
 * @author zxw
 * @date 2023-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/formData")
public class FormDataController extends BaseController {

    private final IFormDataService iFormDataService;

    /**
     * 查询单提交数据列表
     */
    @SaCheckPermission("flowable:formData:list")
    @GetMapping("/list")
    public TableDataInfo<FormDataVo> list(FormDataBo bo, PageQuery pageQuery) {
        return iFormDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单提交数据列表
     */
    @SaCheckPermission("flowable:formData:export")
    @Log(title = "单提交数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FormDataBo bo, HttpServletResponse response) {
        List<FormDataVo> list = iFormDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "单提交数据", FormDataVo.class, response);
    }

    /**
     * 获取单提交数据详细信息
     *
     * @param dataId 主键
     */
    @SaCheckPermission("flowable:formData:query")
    @GetMapping("/{dataId}")
    public R<FormDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dataId) {
        return R.ok(iFormDataService.queryById(dataId));
    }

    /**
     * 新增单提交数据
     */
    @SaCheckPermission("flowable:formData:add")
    @Log(title = "单提交数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FormDataBo bo) {
        return toAjax(iFormDataService.insertByBo(bo));
    }

    /**
     * 修改单提交数据
     */
    @SaCheckPermission("flowable:formData:edit")
    @Log(title = "单提交数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FormDataBo bo) {
        return toAjax(iFormDataService.updateByBo(bo));
    }

    /**
     * 删除单提交数据
     *
     * @param dataIds 主键串
     */
    @SaCheckPermission("flowable:formData:remove")
    @Log(title = "单提交数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dataIds) {
        return toAjax(iFormDataService.deleteWithValidByIds(Arrays.asList(dataIds), true));
    }
}
